from collections import *
from operator import itemgetter
import random
import numpy as np
import pandas as pd  
class zlsx():
    def __init__(self,NNH,NNL,conditions):
        self.NNH=NNH
        self.NNL=NNL

        self.conditions=conditions

        self.maxnum=-1000
        self.NNlistHA=[i for i in range(int(NNH))]
        self.NNlistLA=[i for i in range(int(NNL))]
        
        self.coil_queA=deque()

   
    def solve(self,board, num):  

        if num > self.NNH*self.NNL-1 :   #退出条件（阵列满足时退出递归）
            print(f"\n阵列生成成功！\n")
            return True  

        # 使用列表推导式和集合来创建补集
        q_row=[q[1] for q in self.coil_queA]
        q_col=[q[2] for q in self.coil_queA]
        rowlist = [item for item in set(self.NNlistHA) if item not in set(q_row)]
        collist = [item for item in set(self.NNlistLA) if item not in set(q_col)]
        
        #随机扫描！！！
        for row in np.random.permutation(rowlist):
            for col in np.random.permutation(collist): 

                if board[row][col] == -1 :
                        
                        
                    # 尝试填入数字与 队列
                    board[row][col] = num 
                    if len(self.coil_queA)>= self.conditions:
                        old_coil_queA=self.coil_queA.popleft()
                    self.coil_queA.append([num,row,col])
    

                    # 递归填入下一个数字  
                    if self.solve(board, num + 1):                
                        return True  
                    # 回溯，撤销填入 
                    else:
                        board[row][col] = -1 
                        if len(self.coil_queA)>= self.conditions:
                            self.coil_queA.appendleft(old_coil_queA)
                        self.coil_queA.pop()
                        
                        if(num>self.maxnum):
                            self.maxnum=num
                            print(f"阵列生成中！ 如果等待时间过长，请重试，使用新的随机效果来生成阵列排布！")

       
        return False  
  

def is_valid2(NNH,NNL,board, num, row, col,conditions):  
    NNlistHA=[i for i in range(int(NNH))]
    NNlistLA=[i for i in range(int(NNL))]
    

    for condition in range(1,conditions+1):
        for i in NNlistHA:  #n+1线圈不可以位于 n线圈的同一列
            if board[i][col] == num-condition:  
                print(f'{num}, {row}, {col},{board[row][i]},{board[i][col]},{i},{num-condition}')
                return False  
        for i in NNlistLA:  #n+1线圈不可以位于 n线圈的同一行
            if board[row][i] == num-condition:
                print(f'{num}, {row}, {col},{board[row][i]},{board[i][col]},{i},{num-condition}')
                return False  



    return True 


def 显示阵列(board,NNH,NNL,conditions=-1,阵列路数=1,子阵列=''):
    if conditions !=-1:
        print(f"{子阵列} 的阵列规模：{阵列路数}*{NNH}*{NNL} | 满足条件数：{conditions}")
    else:
        print(f"阵列规模：{阵列路数}*{NNH}*{NNL}")



    

    # 所有数字都已填入，打印结果  
    hn=0    
    for row in board:
        Array_H = NNH
        hh=hn//Array_H
        hmod= hn%Array_H
        #print(f'{Array_H},{hh},{hmod}')
        H_xs =['HA','HB','HC','HD','HE','HF','HG','HH','HI']   
        L_xs =['LA','LB','LC','LD','LE','LF','LG','LH','LI']   

        print(f"{H_xs[hh]}%1s: ["%hmod ,end="")



        hn+=1
        for rowi in row:
            if rowi == -1:
                rowi ='x'
                print("%3s, "%rowi,end="")  
            else:
                print("%3d, "%rowi,end="")  
        print("],\n")      
        
    Lstr='      '

    listA= list(range(NNL*阵列路数))
    for i in listA:

        Array_L = NNL
        ll=i//Array_L
        lmod= i%Array_L
        #print(f'{Array_H},{hh},{hmod}') 
        #print(f"{H_xs[hh]}%1s: ["%hmod ,end="")

        Lstr+=f" {L_xs[ll]}%1d "%lmod

        #Lstr+=" L%2d "%i
    print(Lstr)
    print('\n')


def 根据原有数据生成阵列(zlsx_输入 ='空缺'):
    if zlsx_输入=='[]' or zlsx_输入=='空缺' :
        return -1, -1, -1, []


    zlsx_all=[['1*4*4',	[[0, 1, 0], [1, 2, 2], [2, 0, 3], [3, 1, 1], [4, 3, 0], [5, 0, 2], [6, 2, 1], [7, 3, 3], [8, 0, 0], [9, 1, 2], [10, 3, 1], [11, 2, 0], [12, 1, 3], [13, 3, 2], [14, 0, 1], [15, 2, 3]] ],
['1*4*5',	[[0, 0, 1], [1, 3, 3], [2, 2, 0], [3, 1, 4], [4, 0, 2], [5, 2, 3], [6, 3, 1], [7, 0, 4], [8, 1, 2], [9, 2, 1], [10, 3, 4], [11, 0, 3], [12, 1, 1], [13, 3, 0], [14, 2, 2], [15, 1, 3], [16, 0, 0], [17, 3, 2], [18, 2, 4], [19, 1, 0]]],
['1*5*5',	[[0, 0, 3], [1, 1, 4], [2, 3, 1], [3, 4, 2], [4, 0, 0], [5, 1, 3], [6, 3, 4], [7, 4, 1], [8, 2, 0], [9, 0, 2], [10, 3, 3], [11, 1, 1], [12, 2, 4], [13, 4, 0], [14, 3, 2], [15, 0, 1], [16, 2, 3], [17, 4, 4], [18, 1, 2], [19, 3, 0], [20, 2, 1], [21, 0, 4], [22, 4, 3], [23, 1, 0], [24, 2, 2]]],
['1*5*6',	[[0, 3, 2], [1, 4, 0], [2, 0, 4], [3, 1, 5], [4, 2, 3], [5, 3, 1], [6, 0, 0], [7, 4, 5], [8, 2, 2], [9, 1, 1], [10, 0, 3], [11, 3, 4], [12, 4, 2], [13, 2, 5], [14, 1, 3], [15, 0, 1], [16, 3, 0], [17, 2, 4], [18, 1, 2], [19, 4, 1], [20, 0, 5], [21, 3, 3], [22, 2, 0], [23, 1, 4], [24, 0, 2], [25, 4, 3], [26, 2, 1], [27, 1, 0], [28, 3, 5], [29, 4, 4]]],
['1*6*6',	[[0, 4, 5], [1, 0, 1], [2, 2, 2], [3, 1, 4], [4, 3, 0], [5, 4, 3], [6, 5, 5], [7, 2, 1], [8, 1, 2], [9, 3, 4], [10, 4, 0], [11, 5, 3], [12, 2, 5], [13, 0, 2], [14, 1, 1], [15, 4, 4], [16, 5, 0], [17, 2, 3], [18, 0, 5], [19, 3, 2], [20, 4, 1], [21, 5, 4], [22, 2, 0], [23, 0, 3], [24, 1, 5], [25, 3, 1], [26, 5, 2], [27, 2, 4], [28, 0, 0], [29, 1, 3], [30, 3, 5], [31, 4, 2], [32, 5, 1], [33, 0, 4], [34, 1, 0], [35, 3, 3]]],
['1*6*7',	[[0, 1, 0], [1, 0, 1], [2, 2, 3], [3, 5, 6], [4, 4, 5], [5, 1, 2], [6, 3, 4], [7, 0, 0], [8, 5, 3], [9, 4, 6], [10, 1, 1], [11, 2, 2], [12, 0, 4], [13, 3, 3], [14, 4, 0], [15, 5, 5], [16, 2, 1], [17, 0, 2], [18, 1, 3], [19, 3, 6], [20, 5, 0], [21, 2, 4], [22, 0, 5], [23, 4, 1], [24, 1, 6], [25, 5, 2], [26, 3, 0], [27, 2, 5], [28, 0, 3], [29, 4, 4], [30, 5, 1], [31, 3, 2], [32, 2, 6], [33, 1, 5], [34, 4, 3], [35, 5, 4], [36, 3, 1], [37, 0, 6], [38, 2, 0], [39, 4, 2], [40, 1, 4], [41, 3, 5]]],
['1*7*7',	[[0, 2, 2], [1, 5, 1], [2, 0, 3], [3, 1, 6], [4, 3, 5], [5, 4, 4], [6, 6, 0], [7, 2, 1], [8, 0, 2], [9, 1, 3], [10, 3, 6], [11, 5, 4], [12, 4, 0], [13, 6, 5], [14, 0, 1], [15, 1, 2], [16, 3, 3], [17, 2, 6], [18, 5, 0], [19, 4, 5], [20, 0, 4], [21, 1, 1], [22, 3, 2], [23, 6, 6], [24, 2, 0], [25, 4, 3], [26, 5, 5], [27, 1, 4], [28, 3, 1], [29, 6, 2], [30, 0, 0], [31, 4, 6], [32, 5, 3], [33, 1, 5], [34, 2, 4], [35, 6, 1], [36, 3, 0], [37, 4, 2], [38, 5, 6], [39, 0, 5], [40, 2, 3], [41, 6, 4], [42, 1, 0], [43, 4, 1], [44, 5, 2], [45, 0, 6], [46, 2, 5], [47, 6, 3], [48, 3, 4]]],
['1*7*8',	[[0, 2, 7], [1, 4, 1], [2, 1, 3], [3, 3, 5], [4, 5, 2], [5, 0, 0], [6, 6, 7], [7, 4, 6], [8, 1, 4], [9, 3, 1], [10, 5, 3], [11, 0, 2], [12, 6, 5], [13, 2, 0], [14, 1, 6], [15, 3, 7], [16, 5, 1], [17, 0, 4], [18, 6, 2], [19, 2, 3], [20, 4, 5], [21, 3, 6], [22, 1, 7], [23, 5, 0], [24, 6, 1], [25, 2, 2], [26, 0, 3], [27, 3, 4], [28, 1, 5], [29, 4, 0], [30, 6, 6], [31, 2, 1], [32, 0, 7], [33, 5, 4], [34, 1, 2], [35, 4, 3], [36, 3, 0], [37, 2, 5], [38, 0, 1], [39, 5, 6], [40, 6, 4], [41, 4, 2], [42, 1, 0], [43, 3, 3], [44, 0, 5], [45, 5, 7], [46, 2, 6], [47, 4, 4], [48, 6, 0], [49, 3, 2], [50, 1, 1], [51, 5, 5], [52, 0, 6], [53, 4, 7], [54, 2, 4], [55, 6, 3]]],
['1*8*8',	[[0, 3, 5], [1, 7, 1], [2, 5, 7], [3, 6, 2], [4, 4, 0], [5, 2, 3], [6, 1, 4], [7, 0, 6], [8, 7, 5], [9, 3, 7], [10, 6, 1], [11, 5, 0], [12, 2, 2], [13, 1, 3], [14, 0, 4], [15, 4, 5], [16, 3, 6], [17, 6, 7], [18, 7, 0], [19, 5, 1], [20, 1, 2], [21, 2, 4], [22, 4, 3], [23, 0, 5], [24, 6, 6], [25, 7, 7], [26, 3, 0], [27, 1, 1], [28, 5, 2], [29, 4, 4], [30, 0, 3], [31, 6, 5], [32, 7, 6], [33, 2, 0], [34, 1, 7], [35, 3, 2], [36, 5, 4], [37, 0, 1], [38, 6, 3], [39, 4, 6], [40, 2, 5], [41, 1, 0], [42, 7, 2], [43, 3, 4], [44, 0, 7], [45, 5, 3], [46, 4, 1], [47, 2, 6], [48, 1, 5], [49, 6, 0], [50, 7, 4], [51, 0, 2], [52, 3, 3], [53, 4, 7], [54, 2, 1], [55, 5, 5], [56, 1, 6], [57, 6, 4], [58, 0, 0], [59, 7, 3], [60, 4, 2], [61, 2, 7], [62, 3, 1], [63, 5, 6]]],
['1*8*9',	[[0, 5, 4], [1, 3, 7], [2, 1, 8], [3, 2, 0], [4, 6, 3], [5, 7, 1], [6, 0, 5], [7, 5, 2], [8, 3, 6], [9, 1, 4], [10, 4, 0], [11, 2, 8], [12, 7, 3], [13, 0, 7], [14, 5, 1], [15, 3, 2], [16, 6, 5], [17, 4, 6], [18, 2, 4], [19, 7, 0], [20, 1, 7], [21, 0, 1], [22, 5, 3], [23, 3, 8], [24, 4, 5], [25, 2, 2], [26, 7, 6], [27, 6, 0], [28, 0, 4], [29, 5, 7], [30, 3, 3], [31, 4, 8], [32, 1, 1], [33, 2, 5], [34, 7, 2], [35, 6, 4], [36, 0, 0], [37, 5, 6], [38, 4, 7], [39, 1, 3], [40, 3, 5], [41, 2, 1], [42, 7, 8], [43, 6, 2], [44, 5, 0], [45, 4, 4], [46, 1, 6], [47, 0, 3], [48, 3, 1], [49, 7, 7], [50, 6, 8], [51, 5, 5], [52, 4, 2], [53, 1, 0], [54, 0, 6], [55, 3, 4], [56, 2, 7], [57, 6, 1], [58, 7, 5], [59, 4, 3], [60, 1, 2], [61, 0, 8], [62, 3, 0], [63, 2, 6], [64, 6, 7], [65, 7, 4], [66, 4, 1], [67, 1, 5], [68, 0, 2], [69, 5, 8], [70, 2, 3], [71, 6, 6]]],
['1*9*9',	[[0, 3, 3], [1, 8, 8], [2, 4, 5], [3, 2, 7], [4, 5, 4], [5, 6, 1], [6, 1, 2], [7, 7, 6], [8, 0, 0], [9, 3, 8], [10, 8, 3], [11, 2, 5], [12, 5, 7], [13, 6, 4], [14, 1, 1], [15, 7, 2], [16, 0, 6], [17, 4, 8], [18, 3, 0], [19, 2, 3], [20, 8, 5], [21, 6, 7], [22, 1, 4], [23, 5, 1], [24, 0, 2], [25, 4, 6], [26, 7, 0], [27, 2, 8], [28, 3, 5], [29, 6, 3], [30, 1, 7], [31, 8, 4], [32, 0, 1], [33, 5, 6], [34, 4, 2], [35, 7, 8], [36, 2, 0], [37, 6, 5], [38, 1, 3], [39, 3, 4], [40, 0, 7], [41, 8, 6], [42, 5, 2], [43, 7, 1], [44, 4, 0], [45, 6, 8], [46, 1, 5], [47, 2, 4], [48, 0, 3], [49, 3, 6], [50, 8, 2], [51, 7, 7], [52, 4, 1], [53, 6, 0], [54, 1, 8], [55, 5, 5], [56, 0, 4], [57, 2, 6], [58, 3, 2], [59, 7, 3], [60, 8, 1], [61, 4, 7], [62, 1, 0], [63, 5, 8], [64, 0, 5], [65, 6, 6], [66, 2, 2], [67, 7, 4], [68, 3, 1], [69, 8, 7], [70, 4, 3], [71, 5, 0], [72, 0, 8], [73, 1, 6], [74, 6, 2], [75, 7, 5], [76, 2, 1], [77, 3, 7], [78, 4, 4], [79, 8, 0], [80, 5, 3]]],
['1*9*10',	[[0, 7, 9], [1, 0, 1], [2, 1, 7], [3, 5, 2], [4, 6, 4], [5, 3, 0], [6, 4, 6], [7, 2, 3], [8, 7, 8], [9, 8, 1], [10, 0, 5], [11, 1, 9], [12, 6, 2], [13, 3, 4], [14, 4, 0], [15, 2, 6], [16, 5, 3], [17, 8, 7], [18, 7, 1], [19, 1, 8], [20, 0, 2], [21, 3, 5], [22, 4, 9], [23, 2, 0], [24, 6, 6], [25, 5, 7], [26, 8, 4], [27, 1, 3], [28, 7, 2], [29, 0, 8], [30, 3, 1], [31, 2, 9], [32, 6, 5], [33, 4, 7], [34, 5, 0], [35, 1, 4], [36, 8, 3], [37, 7, 6], [38, 3, 8], [39, 0, 9], [40, 2, 2], [41, 6, 1], [42, 5, 5], [43, 4, 4], [44, 8, 0], [45, 1, 6], [46, 3, 7], [47, 7, 3], [48, 2, 8], [49, 6, 9], [50, 5, 1], [51, 0, 4], [52, 8, 5], [53, 1, 2], [54, 3, 6], [55, 4, 3], [56, 2, 7], [57, 6, 8], [58, 5, 9], [59, 0, 0], [60, 7, 4], [61, 1, 5], [62, 8, 2], [63, 3, 3], [64, 2, 1], [65, 4, 8], [66, 6, 7], [67, 0, 6], [68, 7, 0], [69, 5, 4], [70, 8, 9], [71, 3, 2], [72, 2, 5], [73, 4, 1], [74, 6, 3], [75, 0, 7], [76, 1, 0], [77, 5, 8], [78, 8, 6], [79, 3, 9], [80, 7, 5], [81, 4, 2], [82, 2, 4], [83, 0, 3], [84, 1, 1], [85, 6, 0], [86, 8, 8], [87, 5, 6], [88, 7, 7], [89, 4, 5]]],
['1*10*10',	[[0, 9, 1], [1, 3, 4], [2, 8, 5], [3, 5, 0], [4, 0, 6], [5, 6, 7], [6, 2, 9], [7, 7, 2], [8, 4, 3], [9, 1, 8], [10, 9, 4], [11, 3, 5], [12, 5, 1], [13, 0, 0], [14, 6, 6], [15, 8, 7], [16, 7, 9], [17, 2, 2], [18, 1, 3], [19, 4, 8], [20, 9, 5], [21, 3, 1], [22, 0, 4], [23, 5, 6], [24, 8, 0], [25, 7, 7], [26, 6, 9], [27, 2, 3], [28, 1, 2], [29, 4, 5], [30, 9, 8], [31, 0, 1], [32, 3, 6], [33, 8, 4], [34, 7, 0], [35, 5, 9], [36, 6, 3], [37, 1, 7], [38, 4, 2], [39, 2, 8], [40, 0, 5], [41, 9, 6], [42, 8, 1], [43, 7, 4], [44, 3, 9], [45, 5, 3], [46, 1, 0], [47, 6, 2], [48, 4, 7], [49, 2, 5], [50, 0, 8], [51, 8, 6], [52, 7, 1], [53, 9, 9], [54, 3, 3], [55, 1, 4], [56, 5, 2], [57, 4, 0], [58, 2, 7], [59, 6, 5], [60, 8, 8], [61, 7, 6], [62, 0, 9], [63, 9, 3], [64, 1, 1], [65, 3, 2], [66, 4, 4], [67, 2, 0], [68, 5, 7], [69, 6, 8], [70, 7, 5], [71, 8, 9], [72, 0, 3], [73, 1, 6], [74, 9, 2], [75, 4, 1], [76, 2, 4], [77, 3, 7], [78, 6, 0], [79, 7, 8], [80, 5, 5], [81, 8, 3], [82, 1, 9], [83, 0, 2], [84, 4, 6], [85, 2, 1], [86, 9, 7], [87, 3, 0], [88, 6, 4], [89, 5, 8], [90, 7, 3], [91, 1, 5], [92, 8, 2], [93, 4, 9], [94, 2, 6], [95, 0, 7], [96, 9, 0], [97, 6, 1], [98, 3, 8], [99, 5, 4]]] ]
    
    zlsx_all_dice={}
    for 阵列规模,阵列排布 in zlsx_all:
        zlsx_all_dice[阵列规模]=阵列排布

    numbers = zlsx_输入.split('*')
    Array_n, Array_H, Array_L = map(int, numbers)


    阵列路数=Array_n
    NNL=Array_L
    NNH=Array_H

    board = [[-1] * (NNL) for _ in range(NNH)]

    for pbi in zlsx_all_dice[ f'1*{Array_H}*{Array_L}'  ] :
        NUM =pbi[0]
        H=  pbi[1]
        L=  pbi[2]
        board[H][L] =NUM

    # 根据路数扩展矩阵
    board2 = [[-1] * (NNL*阵列路数) for _ in range(NNH*阵列路数)]  # 初始化NNxNN的二维数组  
    for row in range(NNH):
        for col in range(NNL):
            for n in range(阵列路数):
                board2[row+n*NNH][col+n*NNL]= board[row][col] *阵列路数 +n

    显示阵列(board2,NNH,NNL,-1,阵列路数,'')

    q_result=[]
    for row in range(NNH*阵列路数):
        for col in range(NNL*阵列路数):
            if board2[row][col] !=-1 :
                q_result.append([board2[row][col],row,col])

    # 按照线圈编号进行排序
    board_list = sorted(q_result)

    #print( f"\n输出阵列排布，以供程序使用：\n{board_list}\n")
    print( f"\n按线圈编号输出其高侧和低侧位置")
    print( f" i  H  L")
    for blist in board_list:
        hh=blist[1]%Array_H
        ll=blist[2]%Array_L

        H_xs =['HA','HB','HC','HD','HE','HF','HG','HH','HI']   
        L_xs =['LA','LB','LC','LD','LE','LF','LG','LH','LI']   
        ii=blist[0]%阵列路数
        print([blist[0],f'{H_xs[ii]}{hh}' ,f'{L_xs[ii]}{ll}' ])

    return Array_n, Array_H, Array_L,board_list
    
def main(): 
    print("本程序适用于n阵列，如n*4*5规模的阵列等")

    #根据原有数据生成阵列('3*5*5')


    阵列路数=int(input("请输入阵列分路数量："))

    #输入阵列排布=[[0, 1, 0], [1, 2, 2], [2, 0, 3], [3, 1, 1], [4, 3, 0], [5, 0, 2], [6, 2, 1], [7, 3, 3], [8, 0, 0], [9, 1, 2], [10, 3, 1], [11, 2, 0], [12, 1, 3], [13, 3, 2], [14, 0, 1], [15, 2, 3]]   
    #输入阵列排布 =[[0, 4, 5], [1, 0, 1], [2, 2, 2], [3, 1, 4], [4, 3, 0], [5, 4, 3], [6, 5, 5], [7, 2, 1], [8, 1, 2], [9, 3, 4], [10, 4, 0], [11, 5, 3], [12, 2, 5], [13, 0, 2], [14, 1, 1], [15, 4, 4], [16, 5, 0], [17, 2, 3], [18, 0, 5], [19, 3, 2], [20, 4, 1], [21, 5, 4], [22, 2, 0], [23, 0, 3], [24, 1, 5], [25, 3, 1], [26, 5, 2], [27, 2, 4], [28, 0, 0], [29, 1, 3], [30, 3, 5], [31, 4, 2], [32, 5, 1], [33, 0, 4], [34, 1, 0], [35, 3, 3]]
    #输入阵列排布= eval(input())
    输入阵列排布= 'n'

    if 输入阵列排布 != 'n' :
        NNH = max( [pbi[1]  for pbi in 输入阵列排布 ]   ) +1
        NNL = max( [pbi[2]  for pbi in 输入阵列排布 ]   ) +1
        conditions=NNH-2   #满足条件数 ，理论上最大满足NN-2个条件
        
        print(f'NNH：{NNH} NNL{NNL}')

        board = [[-1] * (NNL) for _ in range(NNH)]

        for pbi in 输入阵列排布 :
            NUM =pbi[0]
            H=  pbi[1]
            L=  pbi[2]
            board[H][L] =NUM

        
    else :
        NNH=int(input("请输入子阵列高侧数量（短边）："))
        NNL=int(input("请输入子阵列低侧数量（长边）：")) 

        conditions=NNH-2   #满足条件数 ，理论上最大满足NN-2个条件

        zl=zlsx(NNH,NNL,conditions)

        board = [[-1] * (NNL) for _ in range(NNH)]  # 初始化NNxNN的二维数组  
        if not zl.solve(board, 0):  
            print("No solution found.")  


  
    显示阵列(board,NNH,NNL,conditions,1,'子阵列')

    '''######
    q_result=[]
    for row in range(NNH):
        for col in range(NNL):
            if board[row][col] !=-1 :
                q_result.append([board[row][col],row,col])

    # 按照线圈编号进行排序
    board_list = sorted(q_result)

    print( f"\n验证线圈排布是否满足{conditions}个条件数")
    for b in board_list:
        if(is_valid2(NNH,NNL,board, b[0], b[1], b[2],conditions)):
            print( f"线圈：{b[0]} 验证成功！")
        else:    
            print( f"线圈：{b[0]} 验证错误！")
    ####'''

    # 根据路数扩展矩阵
    board2 = [[-1] * (NNL*阵列路数) for _ in range(NNH*阵列路数)]  # 初始化NNxNN的二维数组  
    for row in range(NNH):
        for col in range(NNL):
            for n in range(阵列路数):
                board2[row+n*NNH][col+n*NNL]= board[row][col] *阵列路数 +n

    显示阵列(board2,NNH,NNL,conditions,阵列路数,'扩展后的完整')


    q_result=[]
    for row in range(NNH*阵列路数):
        for col in range(NNL*阵列路数):
            if board2[row][col] !=-1 :
                q_result.append([board2[row][col],row,col])

    # 按照线圈编号进行排序
    board_list = sorted(q_result)

    
    print( f"\n输出阵列排布，以供程序使用：\n{board_list}\n")

    print( f"\n按线圈编号输出其高侧和低侧位置")
    print( f" i    H      L")
    for blist in board_list:
        print(blist)


    
    # 保存阵列文件  
    with open(f'zlsx_{阵列路数}x{NNH}x{NNL}.txt', 'w', encoding='utf-8') as file:  
        file.write(str(board_list))
    print(f'保存阵列文件zlsx_{阵列路数}x{NNH}x{NNL}.txt 保存成功') 

  
if __name__ == "__main__":  
    main()